package com.controller;


import com.bean.pojo.AirQualityIndex;
import com.bean.pojo.District;
import com.bean.vo.AirQualityIndexVo;
import com.service.AirQualityIndexService;
import com.service.DistrictService;
import com.util.PageAssistant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class AirController {

    @Autowired
    private AirQualityIndexService as;
    @Autowired
    private DistrictService ds;

    @RequestMapping("/query")//查也
    public String query(HttpSession session,Integer currPage,Integer districtId){

        if(districtId!=null&&as.query(new AirQualityIndex(districtId))==null){
            return"/page/msg.jsp";
        }
        System.out.println(">>> 查询总记录信息");
        //分页是也,条件也在里面了
        PageAssistant<AirQualityIndexVo> paUser=new PageAssistant();
        Map<String,Object> map=new HashMap<>();
        if(districtId!=null&&districtId!=0){
            map.put("districtId",districtId);
        }
        int count=   as.queryCount(map);
        paUser.setCount(count);
        if(currPage!=null)  {
            paUser.setCurrPage(currPage);
        }
        map.put("start",paUser.getStart());
        map.put("size",paUser.getPageSize());

List<District>districtlist=ds.query(new District());
session.setAttribute("districtlist",districtlist);
        List<AirQualityIndexVo> tl=  as.queryByPage(map);
        if(tl==null){
            return"/page/msg.jsp";
        }
        paUser.setItems(tl);

        session.setAttribute("paUser",paUser);
        return "/page/list.jsp";
    }

    @RequestMapping("/toadd")//加也
    public String toadd(HttpSession session){
        District d=new District();
     List<District>dl=   ds.query(d);

     session.setAttribute("districtlist",dl);
        return "/page/add.jsp";
    }

    @RequestMapping("/doadd")//加也
    public String doadd(HttpSession session,AirQualityIndex airQualityIndex){
        airQualityIndex.setLastModifyTime(new Date());

        int i=  as.save(airQualityIndex);
        if(i!=1){
            String msg="添加失败,请重试";
            session.setAttribute("msg",msg);
            return "/page/add.jsp";
        }
        String msg="添加成功";
        session.setAttribute("msg",msg);

        return "redirect:/query";
    }

    @RequestMapping("/remove")//删除也
    public String remove(HttpSession session,Integer id){
        AirQualityIndex a=new AirQualityIndex();
        a.setId(id);

        int i=  as.remove(a);
        if(i!=1){
            String msg="删除失败,请重试";
            session.setAttribute("msg",msg);
            return "/page/list.jsp";
        }else {
            String msg="删除成功";
            session.setAttribute("msg",msg);


            return "redirect:/query";
        }


    }

    @RequestMapping("/domodify")//修改
    public String domodify(HttpSession session,AirQualityIndex airQualityIndex){
      int i=  as.modify(airQualityIndex);

       if(i!=0){
           String msg="修改成功";
           session.setAttribute("msg",msg);
           return "redirect:/query";
       }
        else{
           String msg="进入修改失败,请重试";
           session.setAttribute("msg",msg);
           return "redirect:/query";
       }

    }
    @RequestMapping("/tomodify")//修改,实则调到详情页也，点击修改方能修改
    public String tomodify(HttpSession session,Integer id){
        AirQualityIndex a=new AirQualityIndex();
        a.setId(id);

        List<AirQualityIndexVo>al=  as.query(a);

        List<District>districtlist=ds.query(new District());
        session.setAttribute("districtlist",districtlist);
        if(al!=null){
            AirQualityIndexVo av=    al.get(0);
            session.setAttribute("av",av);
            return "/page/modify.jsp";
        }
        else{
            String msg="进入修改失败,请重试";
            session.setAttribute("msg",msg);
            return "redirect:/query";
        }

    }

}
